import Love from "../love/love"
import db from "../data/car"
import playerData from "../data/player"
var love = new Love("dealer")

love.setCarData = function() {
  let i = love.index
  let data = db[i]
  let ui = love.uiData
  ui[1].obj.text = playerData.money + " ￥"
  ui[2].obj.image = love.loader.data.sheet.extend(data.quad)
  ui[4].obj.text = data.name
  ui[6].obj.text = data.type
  ui[7].data = data.enginepower
  ui[8].data = data.structure
  ui[9].data = data.streamline
  ui[10].data = data.fueltank
  ui[11].data = data.n2opower
  ui[12].data = data.itemslot
  ui[14].obj.text = data.price + "￥"
  love.sum = data.price
  for (var j in playerData.slot){
    if (playerData.slot[j].type == i) {
      ui[15].obj.text = "持有"
      ui[15].obj.available = false
      return
    }
  }
  ui[15].obj.text = "购买"
  ui[15].obj.available = true
}

love.payment = function () {
  if (playerData.money >= love.sum) {
    playerData.money -= love.sum
    playerData.slot.push({
      type: love.index, upgrade: {
        structure: 0,
        streamline: 0,
        enginepower: 0,
        fueltank: 0,
        n2ovol: 0,
        n2opower: 0,
        itemslot: 0
      }
    })
  }
  love.setCarData()
}

love.enter = function () {
  love.graphics.setDesignResolution(1600, 900)
  var ui = [
    { type: "label", text: "车辆经销商", x: 100, y: 30, w: 300, h: 100, font: love.loader.data.font50 },
    { type: "button", text: "账户：999999999￥", x: 1120, y: 50, w: 400, h: 100, font: love.loader.data.font30 },
    { type: "image", x: 800, y: 200, w: 240 * 3, h: 160 * 3, image: love.loader.data.sheet.extend([0, 0, 24, 16]), fillColor: "white" },
    { type: "label", text: "车型", x: 100, y: 200, w: 150, h: 50, font: love.loader.data.font30 },
    { type: "label", text: "未定", x: 300, y: 200, w: 400, h: 50, font: love.loader.data.font30 },
    { type: "label", text: "级别", x: 100, y: 270, w: 150, h: 50, font: love.loader.data.font30 },
    { type: "label", text: "轿车", x: 300, y: 270, w: 400, h: 50, font: love.loader.data.font30 },
    { type: "label", text: "引擎", x: 100, y: 340, w: 150, h: 50, font: love.loader.data.font30 },
    { type: "label", text: "强度", x: 100, y: 410, w: 150, h: 50, font: love.loader.data.font30 },
    { type: "label", text: "流线", x: 100, y: 480, w: 150, h: 50, font: love.loader.data.font30 },
    { type: "label", text: "油箱", x: 100, y: 550, w: 150, h: 50, font: love.loader.data.font30 },
    { type: "label", text: "氮氧", x: 100, y: 620, w: 150, h: 50, font: love.loader.data.font30 },
    { type: "label", text: "物品", x: 100, y: 690, w: 150, h: 50, font: love.loader.data.font30 },
    { type: "label", text: "价格", x: 100, y: 760, w: 150, h: 50, font: love.loader.data.font30 },
    { type: "label", text: "10000$", x: 300, y: 760, w: 400, h: 50, font: love.loader.data.font30 },
    { type: "button", text: "购买", x: 1320, y: 750, w: 200, h: 100, font: love.loader.data.font30,onTouch(){love.payment()} },
    { type: "button", text: "返回", x: 1100, y: 750, w: 200, h: 100, font: love.loader.data.font30,onTouch(){love.switch("main")}},
  ]
  love.ui.load(ui)
  love.uiData = ui
  love.index = 1
  love.setCarData()
}
love.update = function (dt) {
  
}
love.draw = function () {
  for (var i = 7;i<13;++i){
    love.graphics.setColor("white")
    let d = love.uiData[i]
    //console.log(d)
    love.graphics.setLineWidth(1)
    love.graphics.rectangle("line", d.obj.x + 200, d.obj.y,400,50)
    love.graphics.setColor("gray")
    love.graphics.rectangle("fill", d.obj.x + 200, d.obj.y, (d.data[1] / 10) * 400, 50)
    love.graphics.setColor("silver")
    love.graphics.rectangle("fill", d.obj.x + 200, d.obj.y, (d.data[0] / 10) * 400, 50)
  }
}
love.touch.onSwipe = function (touch) {
  if (touch.dx<0) {
    love.index -=1
    if (love.index < 0) {
      love.index = 35
    }
  } else {
    love.index += 1
    if (love.index > 35) {
      love.index = 0
    }
  }
  for (let i = 3;i<15;i++){
    love.timer.newTween(500,love.uiData[i].obj,{x : love.uiData[i].x - 1000}, "Quadratic.Out",
      function(){
        love.setCarData()
        love.timer.newTween(500,love.uiData[i].obj,{x: love.uiData[i].x}, "Quadratic.Out")
      }
    )
  }
  love.timer.newTween(500, love.uiData[2].obj, { x: love.uiData[2].x + 1000 }, "Quadratic.Out",
    function () {
      love.setCarData()
      love.timer.newTween(500, love.uiData[2].obj, { x: love.uiData[2].x }, "Quadratic.Out")
    }
  )
}

export default love